home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / Scripts / pilprint.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  2.1 KB  |  93 lines

  1. #! /usr/local/bin/python
  2. #
  3. # The Python Imaging Library.
  4. # $Id: pilprint.py,v 1.1.1.1 1998/08/18 13:07:58 sjoerd Exp $
  5. #
  6. # print image files to postscript printer
  7. #
  8. # History:
  9. # 0.1    96-04-20 fl    Created
  10. # 0.2a1    96-10-04 fl    Use draft mode when converting.
  11. #
  12.  
  13. VERSION = "pilprint 0.2a1/96-10-04"
  14.  
  15. import Image
  16. import PSDraw
  17.  
  18. letter = ( 1.0*72, 1.0*72, 7.5*72, 10.0*72 )
  19.  
  20. def description(file, image):
  21.     import os
  22.     title = os.path.splitext(os.path.split(file)[1])[0]
  23.     format = " (%dx%d "
  24.     if image.format:
  25.     format = " (" + image.format + " %dx%d "
  26.     return title + format % image.size + image.mode + ")"
  27.  
  28. import getopt, os, sys
  29.  
  30. if len(sys.argv) == 1:
  31.     print "PIL Print 0.2a1/96-10-04 -- print image files"
  32.     print "Usage: pilprint files..."
  33.     print "Options:"
  34.     print "  -c            colour printer (default is monochrome)"
  35.     print "  -p            print via lpr (default is stdout)"
  36.     print "  -P <printer>  same as -p but use given printer"
  37.     sys.exit(1)
  38.  
  39. try:
  40.     opt, argv = getopt.getopt(sys.argv[1:], "cdpP:")
  41. except getopt.error, v:
  42.     print v
  43.     sys.exit(1)
  44.  
  45. printer = None # print to stdout
  46. monochrome = 1 # reduce file size for most common case
  47.  
  48. for o, a in opt:
  49.     if o == "-d":
  50.     # debug: show available drivers
  51.         Image.import_plugins()
  52.     print Image.ID
  53.     sys.exit(1)
  54.     elif o == "-c":
  55.     # colour printer
  56.     monochrome = 0
  57.     elif o == "-p":
  58.     # default printer channel
  59.     printer = "lpr"
  60.     elif o == "-P":
  61.     # printer channel
  62.     printer = "lpr -P%s" % v
  63.  
  64. for file in argv:
  65.     try:
  66.  
  67.     im = Image.open(file)
  68.  
  69.     title = description(file, im)
  70.  
  71.     if monochrome and im.mode not in ["1", "L"]:
  72.         im.draft("L", im.size)
  73.         im = im.convert("L")
  74.  
  75.     if printer:
  76.         fp = os.popen(printer, "w")
  77.     else:
  78.         fp = sys.stdout
  79.  
  80.     ps = PSDraw.PSDraw(fp)
  81.  
  82.     ps.begin_document()
  83.     ps.setfont("Helvetica-Narrow-Bold", 18)
  84.     ps.text((letter[0], letter[3]+24), title)
  85.     ps.setfont("Helvetica-Narrow-Bold", 8)
  86.     ps.text((letter[0], letter[1]-30), VERSION)
  87.     ps.image(letter, im)
  88.     ps.end_document()
  89.  
  90.     except:
  91.     print "cannot print image",
  92.     print "(%s:%s)" % (sys.exc_type, sys.exc_value)
  93.